package cn.com.bianguo.java.plugindemo.main.controller;

import cn.com.bianguo.java.plugindemo.pluginimpl.IMessagePlugin;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginManager;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/plugins")
public class PluginController {

    @Autowired
    private PluginManager pluginManager;

    @RequestMapping("/list")
    public List list() {
        List<PluginWrapper> plugins = pluginManager.getPlugins();
        if (CollectionUtils.isEmpty(plugins)) {
            return null;
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (PluginWrapper pluginWrapper : plugins) {
            String pluginId = pluginWrapper.getPluginId();
            Path pluginPath = pluginWrapper.getPluginPath();
            PluginState pluginState = pluginWrapper.getPluginState();
            PluginDescriptor descriptor = pluginWrapper.getDescriptor();
            Map<String, Object> result = new HashMap<>();
            result.put("pluginId", pluginId);
            result.put("pluginPath", pluginPath);
            result.put("pluginState", pluginState);
            result.put("descriptor", descriptor);
            resultList.add(result);
        }
        return resultList;
    }

    @RequestMapping("/info")
    public Map info() {
        PluginWrapper pluginWrapper = pluginManager.getPlugin("aliyun-message-plugin");
        String pluginId = pluginWrapper.getPluginId();
        Path pluginPath = pluginWrapper.getPluginPath();
        PluginState pluginState = pluginWrapper.getPluginState();
        PluginDescriptor descriptor = pluginWrapper.getDescriptor();
        Map<String, Object> result = new HashMap<>();
        result.put("pluginId", pluginId);
        result.put("pluginPath", pluginPath);
        result.put("pluginState", pluginState);
        result.put("descriptor", descriptor);
        return result;
    }

    @RequestMapping("/reload")
    public void reload() {
        pluginManager.stopPlugins();
        pluginManager.unloadPlugins();
        pluginManager.loadPlugins();
        pluginManager.startPlugins();
    }

    @RequestMapping("/unload")
    public void unload() {
        pluginManager.stopPlugins();
        pluginManager.unloadPlugins();
    }

    @RequestMapping("/sendMsg")
    public String sendMsg(String msg, int type) {
        List<IMessagePlugin> plugins = pluginManager.getExtensions(IMessagePlugin.class);
        if (CollectionUtils.isEmpty(plugins)) {
            return "缺少插件";
        }
        for (IMessagePlugin plugin : plugins) {
            int pluginType = plugin.pluginType();
            if (type == pluginType) {
                plugin.sendMsg(msg);
                return "success";
            }
        }
        return "缺少插件";
    }
}
